import { reqUserAddress, reqTradeInfo,reqSubmitOrder} from "@/api";
const state = {
    //用户地址信息
    address: [],
    //交易信息
    tradeInfo:{},
    //订单号
    orderId:''
};
const mutations = {
    GETUSERADDRESS: (state, address) => {
        state.address = address;
    },
    GETTRADEINFO:(state,tradeInfo)=>{
        state.tradeInfo = tradeInfo;
    },
    SUBMITORDER:(state,orderId)=>{
        state.orderId = orderId;
    }

};
const actions = {
    //获取用户地址信息
    getUserAddress: async ({ commit, state, dispatch }) => {
        let result = await reqUserAddress();
        if (result.code == 200) {
            commit("GETUSERADDRESS", result.data);
        }
    },
    //获取交易页信息
    getTradeInfo: async ({ commit, state, dispatch }) => {
        let result = await reqTradeInfo();
         if(result.code==200){
             commit("GETTRADEINFO",result.data);
         }
    },
    //提交订单
    submitOrder:async({commit,state,dispatch},{tradeNo,data})=>{
         let result  = await reqSubmitOrder(tradeNo,data);
         //提交订单【需要信息提交服务器】，返回一个订单号【很重要】
         if(result.code==200){
             commit("SUBMITORDER",result.data);
             console.log(result.data);
             return 'ok';
         }else{
             return Promise.reject();
         }
    }
};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}